<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户">
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="请选择商户"
            filterable
          >
            <el-option
              v-for="item in merchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="商户名称">
          <el-input v-model="form.merchant_name" placeholder="商户名称" />
        </el-form-item> -->
        <el-form-item label="账户名">
          <el-input v-model="form.card_name" placeholder="账户名" />
        </el-form-item>
        <el-form-item label="银行账号">
          <el-input v-model="form.card_number" placeholder="银行账号" />
        </el-form-item>
        <el-form-item label="是否可用">
          <el-select clearable v-model="form.is_active" placeholder="是否可用">
            <el-option label="可用" value="1"></el-option>
            <el-option label="不可用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否黑名单">
          <el-select
            clearable
            v-model="form.is_blacklist"
            placeholder="是否黑名单"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户ID" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column prop="user_kyc_type" label="类型" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.user_kyc_type == 1 ? 'success' : 'danger'"
              >
                {{ scope.row.user_kyc_type == 1 ? "企业" : "个人" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_active" label="是否可用" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_active == 1 ? 'success' : 'danger'">
                {{ scope.row.is_active == 1 ? "可用" : "不可用" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="is_blacklist"
            label="是否黑名单"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="scope.row.is_blacklist == 1 ? 'success' : 'danger'"
              >
                {{ scope.row.is_blacklist == 1 ? "是" : "否" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="账户说明" align="center" />
          <el-table-column prop="bank_name" label="银行名称" align="center" />
          <el-table-column
            prop="bank_address"
            label="银行地址"
            align="center"
          />
          <el-table-column prop="card_number" label="银行账户" align="center" />
          <el-table-column prop="card_name" label="账户名" align="center" />
          <el-table-column prop="swift_code" label="Swift码" align="center" />
          <el-table-column
            prop="payee_address"
            label="收款人地址"
            align="center"
          />
          <el-table-column prop="phone" label="预留手机号" align="center" />
          <el-table-column prop="email" label="预留邮箱" align="center" />
          <el-table-column prop="currency" label="支持币种" align="center" />
          <el-table-column prop="extended" label="补充信息" align="center" />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :companyListData="companyListData"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  customerBankCardListApi,
  customerUpdateBankCardApi,
} from "@/api/ExAssistant/payment";
import DialogForm from "./components/dialogForm.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const merchantList = computed(() => globalStore.merchantList);
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const dialogTitle = ref("");
const companyListData = ref([]);
const formData = {
  merchant_id: "",
  merchant_name: "",
  card_number: "",
  card_name: "",
  is_active: "",
  is_blacklist: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  id: "",
  is_blacklist: "",
  is_active: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    card_number: form.card_number,
    card_name: form.card_name,
    is_active: form.is_active,
    is_blacklist: form.is_blacklist,
  };
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;

  try {
    const res = await customerBankCardListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTableData();
};
// 查询
const onQuery = async () => {
  getTableData();
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    dialogTitle.value = "设置用户银行卡状态";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await customerUpdateBankCardApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTableData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTableData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTableData();
};
onMounted(() => {
  getTableData();
});
</script>
